/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CpuFrame.java
 *
 * Created on 25.09.2011., 16.05.46
 */
package aor2.gui.cpu;

import aor2.components.ElementIOException;
import aor2.cpu.ClockCPU;
import aor2.cpu.InternBus;
import aor2.cpu.blockcontrol.ConditionSignalSet;
import aor2.cpu.blockcontrol.ControlSignalSet;
import aor2.cpu.blockcontrol.ControlUnitMicroProg;
import aor2.cpu.blockcontrol.horizontal.ControlUnitHorizontal2Type;
import java.util.LinkedList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import aor2.cpu.blockinterface.Arbitrator;
import aor2.cpu.blockinterface.BusOvertake;
import aor2.cpu.blockinterface.InterfaceBlock;
import aor2.cpu.blockinterface.SynchronizationBlock;
import aor2.cpu.blockinterrupt.InterruptBlock;
import aor2.cpu.blockinterrupt.InterruptSignalSet;
import aor2.cpu.blockoperation.AlushiftXYM1;
import aor2.cpu.blockoperation.Gate;
import aor2.cpu.blockoperation.Psw;
import aor2.cpu.blockregisters.GPR;
import aor2.cpu.blockregisters.GPRegisters;
import aor2.cpu.blockregisters.InstructionRegister;
import aor2.cpu.blockregisters.InstructionSignal;
import aor2.cpu.blockregisters.TempRegisters;
import aor2.gui.cpu.signals.SignalDialog;
import aor2.gui.cpu.signals.TabeleSignala;
import aor2.memory.TestMem;
import aor2.systembus.ADbus;
import aor2.systembus.SystemBus;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;

/**
 * 
 * @author Moxi
 */
public class CpuFrame extends javax.swing.JFrame {

	private static CpuFrame singleton = null;

	public static CpuFrame getInstance() {
		if (singleton == null) {
			singleton = new CpuFrame();
		}
		return singleton;
	}

	TestMem mem;
	private SignalDialog sigDialog;
	private TabeleSignala sigTable;
        

	/** Creates new form CpuFrame */
	private CpuFrame() {
		controlUnit = ControlUnitHorizontal2Type.getInstance();
		InstructionRegister.IR0.getState();
		InstructionRegister.setCu(controlUnit);
		try {
			Gate.G12.in();
		} catch (ElementIOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		GPRegisters.R0.getState();
		GPR.getInstance();
		InterfaceBlock.getInstance();
		AlushiftXYM1.getInstance();
		GPR.getInstance();
		InterruptBlock.getInstance();
		SynchronizationBlock.getInstance();
		Arbitrator.getInstance();
		BusOvertake.getInstance();
		mem = new TestMem();
		// mem.start();
		initComponents();
		centralPane.setViewportView(currentViewportView = SystemPanel
				.getInstance());
		panelHistory = new LinkedList<JPanel>();
		// sigDialog = new SignalDialog(this, false);
		sigDialog = SignalDialog.getInstance();
		sigDialog.setModal(false);
                sigDialog.setLocation(200,150);
                sigDialog.setTitle("Grafik signala");
		// sigDialog.set
		sigDialog.setVisible(false);
		sigDialog.setAlwaysOnTop(false);
		sigTable = TabeleSignala.getInstance();
                sigTable.setLocation(200, 150);
                sigTable.setTitle("Tabela signala");
		sigTable.setModal(false);
		sigTable.setVisible(false);
		sigTable.setAlwaysOnTop(false);
		pack();
	}

	private ControlUnitMicroProg controlUnit;

	public ControlUnitMicroProg getCU() {
		return controlUnit;
	}

	private LinkedList<JPanel> panelHistory;
	private JPanel currentViewportView;

	public JScrollPane getCentralPane() {
		return centralPane;
	}

	public void setCentralPane(JPanel centralPanel) {
		if (centralPanel == null) {
			return;
		}
		panelHistory.addLast(currentViewportView);
		centralPane.setViewportView(currentViewportView = centralPanel);
		pack();
		repaint();
	}

	public void upOneLevel() {
		JPanel upper = panelHistory.pollLast();
		if (upper != null) {
			currentViewportView = upper;
			centralPane.setViewportView(currentViewportView);
			pack();
		}
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        saveFile = new javax.swing.JFileChooser();
        southPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        microInst = new javax.swing.JTextField();
        clkLab = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        infoArea = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        centralPane = new javax.swing.JScrollPane();
        eastPanel = new javax.swing.JPanel();
        up = new javax.swing.JButton();
        nextClk = new javax.swing.JButton();
        nextIns = new javax.swing.JButton();
        save = new javax.swing.JButton();
        previousClk = new javax.swing.JButton();
        signalButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        itemSave = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        signali = new javax.swing.JMenu();
        signalDialogItem = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        ctrlSigItem = new javax.swing.JMenuItem();
        condSigItem = new javax.swing.JMenuItem();

        saveFile.setDialogType(javax.swing.JFileChooser.SAVE_DIALOG);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("AOR2 Simulator ");

        southPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jScrollPane1.setViewportView(microInst);

        clkLab.setText("CLK");

        jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane2.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

        infoArea.setColumns(20);
        infoArea.setEditable(false);
        infoArea.setRows(5);
        jScrollPane2.setViewportView(infoArea);

        jLabel1.setText("Instrukcija");

        javax.swing.GroupLayout southPanelLayout = new javax.swing.GroupLayout(southPanel);
        southPanel.setLayout(southPanelLayout);
        southPanelLayout.setHorizontalGroup(
            southPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(southPanelLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 312, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(clkLab, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 57, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(133, 133, 133))
        );
        southPanelLayout.setVerticalGroup(
            southPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, southPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(southPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(southPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, southPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(clkLab, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)))
                .addContainerGap())
        );

        getContentPane().add(southPanel, java.awt.BorderLayout.SOUTH);

        centralPane.setBackground(new java.awt.Color(255, 255, 255));
        centralPane.setPreferredSize(null);
        getContentPane().add(centralPane, java.awt.BorderLayout.CENTER);

        eastPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        eastPanel.setMaximumSize(new java.awt.Dimension(300, 200));

        up.setBackground(new java.awt.Color(255, 255, 255));
        up.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aor2/common/resources/up_icon_small.jpg"))); // NOI18N
        up.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        up.setOpaque(false);
        up.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                upActionPerformed(evt);
            }
        });

        nextClk.setText("CLK+");
        nextClk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextClkActionPerformed(evt);
            }
        });

        nextIns.setText("ins+");
        nextIns.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextInsActionPerformed(evt);
            }
        });

        save.setText("Snimi");
        save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });

        previousClk.setText("CLK-");
        previousClk.setEnabled(false);
        previousClk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                previousClkActionPerformed(evt);
            }
        });

        signalButton.setText("Grafik signala");
        signalButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                signalButtonActionPerformed(evt);
            }
        });

        jButton1.setText("Tabele signala");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Memorija");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Registri");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout eastPanelLayout = new javax.swing.GroupLayout(eastPanel);
        eastPanel.setLayout(eastPanelLayout);
        eastPanelLayout.setHorizontalGroup(
            eastPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(eastPanelLayout.createSequentialGroup()
                .addGroup(eastPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(eastPanelLayout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addGroup(eastPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(up, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(previousClk, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(nextClk, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(nextIns, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(eastPanelLayout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(save)))
                .addContainerGap(40, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, eastPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(eastPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                    .addComponent(signalButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE))
                .addGap(20, 20, 20))
        );
        eastPanelLayout.setVerticalGroup(
            eastPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(eastPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(up)
                .addGap(16, 16, 16)
                .addComponent(nextClk)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(previousClk)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(nextIns)
                .addGap(75, 75, 75)
                .addComponent(jButton3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(signalButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 83, Short.MAX_VALUE)
                .addComponent(save)
                .addContainerGap())
        );

        getContentPane().add(eastPanel, java.awt.BorderLayout.EAST);

        jMenu1.setText("Meni");

        itemSave.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        itemSave.setText("Snimi");
        itemSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemSaveActionPerformed(evt);
            }
        });
        jMenu1.add(itemSave);

        jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem3.setText("Izadji");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Clock");

        jMenuItem1.setText("clk+");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        jMenuItem4.setText("clk-");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem4);

        jMenuItem2.setText("ins+");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem2);

        jMenuBar1.add(jMenu2);

        signali.setText("Signali");

        signalDialogItem.setText("grafik signala");
        signalDialogItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                signalDialogItemActionPerformed(evt);
            }
        });
        signali.add(signalDialogItem);
        signali.add(jSeparator1);

        ctrlSigItem.setText("kontrolni signali");
        ctrlSigItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ctrlSigItemActionPerformed(evt);
            }
        });
        signali.add(ctrlSigItem);

        condSigItem.setText("signali uslova");
        condSigItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                condSigItemActionPerformed(evt);
            }
        });
        signali.add(condSigItem);

        jMenuBar1.add(signali);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void upActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_upActionPerformed
		if (up.isEnabled()) {
			up.setEnabled(false);
			upOneLevel();
			up.setEnabled(true);
		}
	}// GEN-LAST:event_upActionPerformed

	private void nextClkActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_nextClkActionPerformed
		if (nextClk.isEnabled()) {
			nextClk.setEnabled(false);
			controlUnit.next();
			controlUnit.next();
			microInst.setText(controlUnit.getuInstr().toString());
			/* (microInst.getText().contains("read"))
				previousClk.setEnabled(false);
			else
				previousClk.setEnabled(true);*/
                        if (ClockCPU.getInstance().getT() == 0)
                            previousClk.setEnabled(false);
                        else
                            previousClk.setEnabled(true);
                        if (controlUnit.getuCnt().getState() == 2 || controlUnit.getuCnt().getState() == 3 || controlUnit.getuCnt().getState() == 0xA || controlUnit.getuCnt().getState() == 0xB)
                            previousClk.setEnabled(false);
                        if (controlUnit.getuCnt().getState() == 0xF || controlUnit.getuCnt().getState() == 0x10 || controlUnit.getuCnt().getState() == 0x17 || controlUnit.getuCnt().getState() == 0x18)
                            previousClk.setEnabled(false);
                        if (controlUnit.getuCnt().getState() == 0x1c || controlUnit.getuCnt().getState() == 0x1d || controlUnit.getuCnt().getState() == 0x22 || controlUnit.getuCnt().getState() == 0x23)
                            previousClk.setEnabled(false);
                        if (controlUnit.getuCnt().getState() == 0xf8 || controlUnit.getuCnt().getState() == 0xf9 || controlUnit.getuCnt().getState() == 0x26 || controlUnit.getuCnt().getState() == 0x27)
                            previousClk.setEnabled(false);
                        if (controlUnit.getuCnt().getState() == 0x2b || controlUnit.getuCnt().getState() == 0x2d || controlUnit.getuCnt().getState() == 0x2C || controlUnit.getuCnt().getState() == 0x2e)
                            previousClk.setEnabled(false);
                        if (controlUnit.getuCnt().getState() == 0x3a || controlUnit.getuCnt().getState() == 0x3d || controlUnit.getuCnt().getState() == 0x33 || controlUnit.getuCnt().getState() == 0x34)
                            previousClk.setEnabled(false);
                        if (controlUnit.getuCnt().getState() == 0x32 || controlUnit.getuCnt().getState() == 0x35 || controlUnit.getuCnt().getState() == 0x3b || controlUnit.getuCnt().getState() == 0x3c)
                            previousClk.setEnabled(false);
                        if (controlUnit.getuCnt().getState() == 0x43 || controlUnit.getuCnt().getState() == 0x44 || controlUnit.getuCnt().getState() == 0x48 || controlUnit.getuCnt().getState() == 0x49)
                            previousClk.setEnabled(false);
                        if (controlUnit.getuCnt().getState() == 0x53 || controlUnit.getuCnt().getState() == 0x4e || controlUnit.getuCnt().getState() == 0x4f || controlUnit.getuCnt().getState() == 0x54)
                            previousClk.setEnabled(false);
                        if (controlUnit.getuCnt().getState() == 0x58 || controlUnit.getuCnt().getState() == 0x59 || controlUnit.getuCnt().getState() == 0x5b || controlUnit.getuCnt().getState() == 0x5c)
                            previousClk.setEnabled(false);
                        if (controlUnit.getuCnt().getState() == 0x60 || controlUnit.getuCnt().getState() == 0x61 || controlUnit.getuCnt().getState() == 0x63 || controlUnit.getuCnt().getState() == 0x64) 
                            previousClk.setEnabled(false);
                        if (controlUnit.getuCnt().getState() == 0x69 || controlUnit.getuCnt().getState() == 0x6a || controlUnit.getuCnt().getState() == 0x6c || controlUnit.getuCnt().getState() == 0x6d)
                            previousClk.setEnabled(false);
                        if (controlUnit.getuCnt().getState() == 0x9c || controlUnit.getuCnt().getState() == 0x9d || controlUnit.getuCnt().getState() == 0x9f || controlUnit.getuCnt().getState() == 0xa0)
                            previousClk.setEnabled(false);
			repaint();
			if (sigDialog.isVisible())
				sigDialog.repaint();
			if (sigTable.isVisible())
				sigTable.repaint();
			nextClk.setEnabled(true);
		}
	}// GEN-LAST:event_nextClkActionPerformed

	private void nextInsActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_nextInsActionPerformed
		if (nextIns.isEnabled()) {
			nextIns.setEnabled(false);
			controlUnit.nextInstruction();
			microInst.setText(controlUnit.getuInstr().toString());
			repaint();
			if (sigDialog.isVisible())
				sigDialog.repaint();
			if (sigTable.isVisible())
				sigTable.repaint();
			nextIns.setEnabled(true);
		}
	}// GEN-LAST:event_nextInsActionPerformed

	private void itemSaveActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_itemSaveActionPerformed
		// TODO add your handling code here:
		save.doClick();
	}// GEN-LAST:event_itemSaveActionPerformed

	public void snimi() {
		save.doClick();
	}

	private void saveActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_saveActionPerformed
		// TODO add your handling code here:
		int returnVal = saveFile.showSaveDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File toSave = saveFile.getSelectedFile();
			toSave.setWritable(true);
			FileWriter output = null;
			try {
				output = new FileWriter(toSave);
				output.append("<#delim#>\n"
						+ ClockCPU.getInstance().createRestorePoint()
						+ "\n<#delim#>\n");
				output.append(InternBus.M1.createRestorePoint()
						+ "\n<#delim#>\n");
				output.append(ConditionSignalSet.getInstance()
						.createRestorePoint() + "\n<#delim#>\n");
				output.append(InterruptSignalSet.getInstance()
						.createRestorePoint() + "\n<#delim#>\n");
				output.append(ControlSignalSet.getInstance()
						.createRestorePoint() + "\n<#delim#>\n");
				output.append(ControlUnitHorizontal2Type.getInstance()
						.createRestorePoint() + "\n<#delim#>\n"); // FIXME ovo
																	// samo kada
																	// je horiz.
																	// uprav.
																	// jed.
				output.append(Arbitrator.getInstance().createRestorePoint()
						+ "\n<#delim#>\n");
				output.append(BusOvertake.getInstance().createRestorePoint()
						+ "\n<#delim#>\n");
				output.append(InterfaceBlock.getInstance().createRestorePoint()
						+ "\n<#delim#>\n");
				output.append(SynchronizationBlock.getInstance()
						.createRestorePoint() + "\n<#delim#>\n");
				output.append(AlushiftXYM1.getInstance().createRestorePoint()
						+ "\n<#delim#>\n");
				output.append(Psw.getInstance().createRestorePoint()
						+ "\n<#delim#>\n");
				output.append(GPRegisters.R0.createRestorePoint()
						+ "\n<#delim#>\n");
				output.append(InstructionRegister.IR0.createRestorePoint()
						+ "\n<#delim#>\n");
				output.append(InterruptBlock.getInstance().createRestorePoint()
						+ "\n<#delim#>\n");
				output.append(TempRegisters.A.createRestorePoint()
						+ "\n<#delim#>\n");
				output.append(ADbus.ABus.createRestorePoint() + "\n<#delim#>\n");
				output.append(SystemBus.getInstance().createRestorePoint()
						+ "\n<#delim#>\n");
				output.append(mem.createRestorePoint() + "\n");
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if (output != null) {
						output.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}// GEN-LAST:event_saveActionPerformed

	private void previousClkActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_previousClkActionPerformed
		if (previousClk.isEnabled()) {
			previousClk.setEnabled(false);
			controlUnit.previousClk();
			microInst.setText(controlUnit.getuInstr().toString());
                        if (ClockCPU.getInstance().getT() == 0)
                            previousClk.setEnabled(false);
                        else
                            previousClk.setEnabled(true);
                         if (controlUnit.getuCnt().getState() == 2 || controlUnit.getuCnt().getState() == 3 || controlUnit.getuCnt().getState() == 0xA || controlUnit.getuCnt().getState() == 0xB)
                            previousClk.setEnabled(false);
                        if (controlUnit.getuCnt().getState() == 0xF || controlUnit.getuCnt().getState() == 0x10 || controlUnit.getuCnt().getState() == 0x17 || controlUnit.getuCnt().getState() == 0x18)
                            previousClk.setEnabled(false);
                        if (controlUnit.getuCnt().getState() == 0x1c || controlUnit.getuCnt().getState() == 0x1d || controlUnit.getuCnt().getState() == 0x22 || controlUnit.getuCnt().getState() == 0x23)
                            previousClk.setEnabled(false);
                        if (controlUnit.getuCnt().getState() == 0xf8 || controlUnit.getuCnt().getState() == 0xf9 || controlUnit.getuCnt().getState() == 0x26 || controlUnit.getuCnt().getState() == 0x27)
                            previousClk.setEnabled(false);
                        if (controlUnit.getuCnt().getState() == 0x2b || controlUnit.getuCnt().getState() == 0x2d || controlUnit.getuCnt().getState() == 0x2C || controlUnit.getuCnt().getState() == 0x2e)
                            previousClk.setEnabled(false);
                        if (controlUnit.getuCnt().getState() == 0x3a || controlUnit.getuCnt().getState() == 0x3d || controlUnit.getuCnt().getState() == 0x33 || controlUnit.getuCnt().getState() == 0x34)
                            previousClk.setEnabled(false);
                        if (controlUnit.getuCnt().getState() == 0x32 || controlUnit.getuCnt().getState() == 0x35 || controlUnit.getuCnt().getState() == 0x3b || controlUnit.getuCnt().getState() == 0x3c)
                            previousClk.setEnabled(false);
                        if (controlUnit.getuCnt().getState() == 0x43 || controlUnit.getuCnt().getState() == 0x44 || controlUnit.getuCnt().getState() == 0x48 || controlUnit.getuCnt().getState() == 0x49)
                            previousClk.setEnabled(false);
                        if (controlUnit.getuCnt().getState() == 0x53 || controlUnit.getuCnt().getState() == 0x4e || controlUnit.getuCnt().getState() == 0x4f || controlUnit.getuCnt().getState() == 0x54)
                            previousClk.setEnabled(false);
                        if (controlUnit.getuCnt().getState() == 0x58 || controlUnit.getuCnt().getState() == 0x59 || controlUnit.getuCnt().getState() == 0x5b || controlUnit.getuCnt().getState() == 0x5c)
                            previousClk.setEnabled(false);
                        if (controlUnit.getuCnt().getState() == 0x60 || controlUnit.getuCnt().getState() == 0x61 || controlUnit.getuCnt().getState() == 0x63 || controlUnit.getuCnt().getState() == 0x64) 
                            previousClk.setEnabled(false);
                        if (controlUnit.getuCnt().getState() == 0x69 || controlUnit.getuCnt().getState() == 0x6a || controlUnit.getuCnt().getState() == 0x6c || controlUnit.getuCnt().getState() == 0x6d)
                            previousClk.setEnabled(false);
                        if (controlUnit.getuCnt().getState() == 0x9c || controlUnit.getuCnt().getState() == 0x9d || controlUnit.getuCnt().getState() == 0x9f || controlUnit.getuCnt().getState() == 0xa0)
                            previousClk.setEnabled(false);
                        
			repaint();
			if (sigDialog.isVisible())
				sigDialog.repaint();
                        
		}
	}// GEN-LAST:event_previousClkActionPerformed

	private void signalButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_signalButtonActionPerformed
		sigDialog.setVisible(true);
		sigDialog.repaint();
		repaint();
	}// GEN-LAST:event_signalButtonActionPerformed

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton1ActionPerformed
		sigTable.setVisible(true);
		sigTable.repaint();
		repaint();
	}// GEN-LAST:event_jButton1ActionPerformed

	private void signalDialogItemActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_signalDialogItemActionPerformed
		sigDialog.setVisible(true);
		sigDialog.repaint();
		repaint();
	}// GEN-LAST:event_signalDialogItemActionPerformed

	private void ctrlSigItemActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_ctrlSigItemActionPerformed
		sigTable.setVisible(true);
		sigTable.repaint();
		repaint();
	}// GEN-LAST:event_ctrlSigItemActionPerformed

	private void condSigItemActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_condSigItemActionPerformed
		sigTable.setVisible(true);
		sigTable.repaint();
		repaint();
	}// GEN-LAST:event_condSigItemActionPerformed

	private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jMenuItem3ActionPerformed
		dispose();
		System.exit(0);
	}// GEN-LAST:event_jMenuItem3ActionPerformed

	private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jMenuItem1ActionPerformed
		nextClkActionPerformed(evt);
	}// GEN-LAST:event_jMenuItem1ActionPerformed

	private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jMenuItem2ActionPerformed
		nextInsActionPerformed(evt);
	}// GEN-LAST:event_jMenuItem2ActionPerformed

	private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jMenuItem4ActionPerformed
		// TODO add your handling code here:
		previousClk.doClick();
	}// GEN-LAST:event_jMenuItem4ActionPerformed

	private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton3ActionPerformed
		// TODO add your handling code here:
		dialog.readOnlyReg();
	}// GEN-LAST:event_jButton3ActionPerformed

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton2ActionPerformed
		// TODO add your handling code here:
		dialog.readOnlyMem();
	}// GEN-LAST:event_jButton2ActionPerformed

	boolean instflag = true;
	public void repaint() {
                setLocation(150,0);
		clkLab.setText("CLK: " + ClockCPU.getInstance().getT());
		InstructionSignal act = InstructionSignal.getActive();
                if (controlUnit.getuCnt().getState() == 0)
                    instflag = false;
                
		if (act != null && act != InstructionSignal.UNKNOWN && instflag) 
			infoArea.setText(InstructionSignal.getActive().name());
                else
                    infoArea.setText("");
                        
                if (ControlSignalSet.getInstance().get("brop").isActive())
                    instflag = true;
                               
                 
		super.repaint();
	}
        
        public JFileChooser getSaveDialog() {
            return saveFile;
        }

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		// <editor-fold defaultstate="collapsed"
		// desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase
		 * /tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
					.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(CpuFrame.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(CpuFrame.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(CpuFrame.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(CpuFrame.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		}
		// </editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				CpuFrame cpu = CpuFrame.getInstance();
				cpu.dialog = new InitDialog(cpu, true);
                                cpu.setLocation(150, 0);
				cpu.setVisible(true);
			}
		});
	}

	private InitDialog dialog;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane centralPane;
    private javax.swing.JLabel clkLab;
    private javax.swing.JMenuItem condSigItem;
    private javax.swing.JMenuItem ctrlSigItem;
    private javax.swing.JPanel eastPanel;
    private javax.swing.JTextArea infoArea;
    private javax.swing.JMenuItem itemSave;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JTextField microInst;
    private javax.swing.JButton nextClk;
    private javax.swing.JButton nextIns;
    private javax.swing.JButton previousClk;
    private javax.swing.JButton save;
    private javax.swing.JFileChooser saveFile;
    private javax.swing.JButton signalButton;
    private javax.swing.JMenuItem signalDialogItem;
    private javax.swing.JMenu signali;
    private javax.swing.JPanel southPanel;
    private javax.swing.JButton up;
    // End of variables declaration//GEN-END:variables
}
